package com.vgooo.client.ui;

import com.vgooo.client.ui.PullDownView.OnTouchListener;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.ScrollView;
/**
 * 解决ScrollView 与 ViewPager 滑动冲突
 * @author Administrator
 *
 */
public class VGScrollView extends ScrollView {
	
	private boolean canSroll ;
	private GestureDetector mGestureDetector ;
//	private View.OnTouchListener mGestureListener ;

	public VGScrollView(Context context) {
		super(context );
		init() ;
		// TODO Auto-generated constructor stub
	}
	public VGScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init() ;
		// TODO Auto-generated constructor stub
	}
	public VGScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init() ;
		// TODO Auto-generated constructor stub
	}
	
	private void init(){
		mGestureDetector = new GestureDetector(new VGScrollDetector()) ;
		canSroll = true ;
	}
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(ev.getAction() == MotionEvent.ACTION_UP){
			canSroll = true ;
		}
		return super.onInterceptTouchEvent(ev)&&mGestureDetector.onTouchEvent(ev);
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(ev);
	}
	
	private class VGScrollDetector extends SimpleOnGestureListener{
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			if (canSroll) {
				if(Math.abs(distanceY)> Math.abs(distanceX)){
					canSroll = true ;
				}else{
					canSroll = false ;
				}
//				log("                  distanceY : " + distanceY + " distanceX : " + distanceX  + "  canSroll : " + canSroll)  ;
			}
//			log("  ****************  distanceY : " + distanceY + " distanceX : " + distanceX  + "  canSroll : " + canSroll)  ;
			return canSroll;
		}
	}
	
	private static void log(String info){
//		Log.i("VGSrollView", info) ;
	}
}
